package com.dianping.cat.home.service.transform;

import com.dianping.cat.home.service.IEntity;
import com.dianping.cat.home.service.IVisitor;
import com.dianping.cat.home.service.entity.Domain;
import com.dianping.cat.home.service.entity.ServiceReport;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;















public class DefaultJsonBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  35 */   public DefaultJsonBuilder() { this(false); }



/*  39 */   public DefaultJsonBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }


  public DefaultJsonBuilder(boolean compact, StringBuilder sb) {
/*  43 */     this.m_compact = compact;
/*  44 */     this.m_sb = sb;
/*  45 */     this.m_visitor = this;
  }

  protected void arrayBegin(String name) {
/*  49 */     indent();
/*  50 */     this.m_sb.append('"').append(name).append(this.m_compact ? "\":[" : "\": [\r\n");
/*  51 */     this.m_level++;
  }

  protected void arrayEnd(String name) {
/*  55 */     this.m_level--;

/*  57 */     trimComma();
/*  58 */     indent();
/*  59 */     this.m_sb.append("],").append(this.m_compact ? "" : "\r\n");
  }

  protected void attributes(Map<String, String> dynamicAttributes, Object... nameValues) {
/*  63 */     int len = nameValues.length;

/*  65 */     for (int i = 0; i + 1 < len; i += 2) {
/*  66 */       Object attrName = nameValues[i];
/*  67 */       Object attrValue = nameValues[i + 1];

/*  69 */       if (attrValue != null) {
/*  70 */         if (attrValue instanceof Collection) {

/*  72 */           Collection<Object> items = (Collection)attrValue;

/*  74 */           if (!items.isEmpty()) {
/*  75 */             indent();
/*  76 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

            for (Object item : items) {
/*  79 */               this.m_sb.append(' ');
/*  80 */               toString(this.m_sb, item);
/*  81 */               this.m_sb.append(',');
            }

/*  84 */             this.m_sb.setLength(this.m_sb.length() - 1);
/*  85 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }
/*  87 */         } else if (attrValue.getClass().isArray()) {
/*  88 */           int length = Array.getLength(attrValue);

/*  90 */           if (length > 0) {
/*  91 */             indent();
/*  92 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  94 */             for (int j = 0; j < length; j++) {
              Object item = Array.get(attrValue, j);
/*  96 */               this.m_sb.append(' ');
/*  97 */               toString(this.m_sb, item);
/*  98 */               this.m_sb.append(',');
            }

/* 101 */             this.m_sb.setLength(this.m_sb.length() - 1);
/* 102 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }

/* 105 */         } else if (this.m_compact) {
/* 106 */           this.m_sb.append('"').append(attrName).append("\":");
/* 107 */           toString(this.m_sb, attrValue);
/* 108 */           this.m_sb.append(",");
        } else {
/* 110 */           indent();
/* 111 */           this.m_sb.append('"').append(attrName).append("\": ");
/* 112 */           toString(this.m_sb, attrValue);
/* 113 */           this.m_sb.append(",\r\n");
        }
      }
    }


/* 119 */     if (dynamicAttributes != null) {
/* 120 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 121 */         if (this.m_compact) {
/* 122 */           this.m_sb.append('"').append((String)e.getKey()).append("\":");
/* 123 */           toString(this.m_sb, e.getValue());
/* 124 */           this.m_sb.append(","); continue;
        }
/* 126 */         indent();
/* 127 */         this.m_sb.append('"').append((String)e.getKey()).append("\": ");
/* 128 */         toString(this.m_sb, e.getValue());
/* 129 */         this.m_sb.append(",\r\n");
      }
    }
  }


  public String build(IEntity<?> entity) {
/* 136 */     objectBegin(null);
/* 137 */     entity.accept(this);
/* 138 */     objectEnd(null);
/* 139 */     trimComma();

/* 141 */     return this.m_sb.toString();
  }

  public String buildArray(Collection<? extends IEntity<?>> entities) {
/* 145 */     this.m_sb.append('[');

/* 147 */     if (entities != null) {
/* 148 */       for (IEntity<?> entity : entities) {
/* 149 */         objectBegin(null);
/* 150 */         entity.accept(this);
/* 151 */         objectEnd(null);
      }

/* 154 */       trimComma();
    }

/* 157 */     this.m_sb.append(']');

/* 159 */     return this.m_sb.toString();
  }

  protected void indent() {
/* 163 */     if (!this.m_compact) {
/* 164 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 165 */         this.m_sb.append("   ");
      }
    }
  }

  protected void objectBegin(String name) {
/* 171 */     indent();

/* 173 */     if (name == null) {
/* 174 */       this.m_sb.append("{").append(this.m_compact ? "" : "\r\n");
    } else {
/* 176 */       this.m_sb.append('"').append(name).append(this.m_compact ? "\":{" : "\": {\r\n");
    }

/* 179 */     this.m_level++;
  }

  protected void objectEnd(String name) {
/* 183 */     this.m_level--;

/* 185 */     trimComma();
/* 186 */     indent();
/* 187 */     this.m_sb.append(this.m_compact ? "}," : "},\r\n");
  }

  protected String toString(Date date, String format) {
/* 191 */     if (date != null) {
/* 192 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 194 */     return null;
  }


  protected void toString(StringBuilder sb, Object value) {
/* 199 */     if (value == null) {
/* 200 */       sb.append("null");
/* 201 */     } else if (value instanceof Boolean || value instanceof Number) {
/* 202 */       sb.append(value);
    } else {
/* 204 */       String val = value.toString();
/* 205 */       int len = val.length();

/* 207 */       sb.append('"');

/* 209 */       for (int i = 0; i < len; i++) {
/* 210 */         char ch = val.charAt(i);

/* 212 */         switch (ch) {
          case '"':
          case '/':
          case '\\':
/* 216 */             sb.append('\\').append(ch);
            break;
          case '\t':
/* 219 */             sb.append("\\t");
            break;
          case '\r':
/* 222 */             sb.append("\\r");
            break;
          case '\n':
/* 225 */             sb.append("\\n");
            break;
          default:
/* 228 */             sb.append(ch);
            break;
        }

      }
/* 233 */       sb.append('"');
    }
  }

  protected void trimComma() {
/* 238 */     int len = this.m_sb.length();

/* 240 */     if (this.m_compact) {
/* 241 */       if (len > 1 && this.m_sb.charAt(len - 1) == ',') {
/* 242 */         this.m_sb.replace(len - 1, len, "");
      }
    }
/* 245 */     else if (len > 3 && this.m_sb.charAt(len - 3) == ',') {
/* 246 */       this.m_sb.replace(len - 3, len - 2, "");
    }
  }




/* 253 */   public void visitDomain(Domain domain) { attributes(null, new Object[] { "id", domain.getId(), "totalCount", Long.valueOf(domain.getTotalCount()), "failureCount", Long.valueOf(domain.getFailureCount()), "failurePercent", Double.valueOf(domain.getFailurePercent()), "sum", Double.valueOf(domain.getSum()), "avg", Double.valueOf(domain.getAvg()), "qps", Double.valueOf(domain.getQps()) }); }



  public void visitServiceReport(ServiceReport serviceReport) {
/* 258 */     attributes(null, new Object[] { "startTime", toString(serviceReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "domain", serviceReport.getDomain(), "endTime", toString(serviceReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 260 */     if (!serviceReport.getDomains().isEmpty()) {
/* 261 */       objectBegin("domains");

/* 263 */       for (Map.Entry<String, Domain> e : serviceReport.getDomains().entrySet()) {
/* 264 */         String key = String.valueOf(e.getKey());

/* 266 */         objectBegin(key);
/* 267 */         ((Domain)e.getValue()).accept(this.m_visitor);
/* 268 */         objectEnd(key);
      }

/* 271 */       objectEnd("domains");
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/service/transform/DefaultJsonBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */